<?php
namespace app\admin\controller;
use app\admin\controller\Common;
class Conf extends Common
{
	
	public function lst(){
		$confRes=db('conf')->order('id')->paginate(10);
		$count=db('conf')->count();
		$this->assign([
            'confRes'=>$confRes,
            'count'=>$count,
        ]);
        return view();
	}
	
	public function add(){
		if(request()->isAjax()){
			$data=input('post.')['data'];
			//如果是有可选项的，替换中文“，”
            if($data['form_type']=='radio' || 'select' || 'checkbox'){
                $data['values']=str_replace('，', ',', $data['values']);
                $data['value']=str_replace('，', ',', $data['value']);
            }
			$add=db('conf')->insert($data);
			if($add){
				return ['msg'=>'新增配置成功','icon'=>1];
			}else{
				return ['msg'=>'新增配置失败','icon'=>2];
			}
		}
		return view();
		
	}
	public function edit(){
 		if(request()->isAjax()){
 			$data=input('post.')['data'];
			//如果是有可选项的，替换中文“，”
            if($data['form_type']=='radio' || 'select' || 'checkbox'){
            	$data['values']=str_replace('，', ',', $data['values']);
                $data['value']=str_replace('，', ',', $data['value']);
            }
 			$save=db('conf')->update($data,['id'=>input('id')]);
 			if($save !== false){
 				return ['msg'=>'编辑配置成功','icon'=>1];
 			}else{
 				return ['msg'=>'编辑配置失败','icon'=>2];
 			}
 			return;
 		}
 		$confs=db('conf')->find(input('id'));
    	$this->assign('confs',$confs);
		return view();
	}
	
	public function del(){
		if(request()->isAjax()){
			$id=input('id');
			$del=db('conf')->delete($id);
			if($del){
				return ['msg'=>'删除配置成功','icon'=>1];
			}else{
				return ['msg'=>'删除配置失败','icon'=>2];
			}
		}
	}
	//批量删除
    public function delAll()
    {
        if(request()->isAjax()){
            $data=input('post.')['data'];
            foreach ($data as $k => $v){
                $del=db('conf')->delete($v);
            }
            return ['msg'=>'批量删除规则成功','icon'=>1];
        } 
    }

	public function conf(){
		if(request()->isPost()){
            $data=input('post.')['data'];
            unset($data['file']);
            
            //接收checkbox 转化为数组 ,键名多一个符号问题解决
            $newdata=[];
            foreach ($data as $k => $v) {
                $newdata[str_replace('[','',$k)]=$v;
            }
            // dump($newdata);die;
            //多选按钮空值处理
            $checkboxArr=db('conf')->field('ename')->where(['form_type'=>'checkbox'])->select();
            foreach ($checkboxArr as $k => $v) {
                if(!isset($newdata[$v['ename']])){
                    $newdata[$v['ename']]='';
                }
            }
            //修改操作
            foreach ($newdata as $k => $v) {
                if(is_array($v)){
                    $value=implode(',', $v);
                    db('conf')->where(['ename'=>$k])->update(['value'=>$value]);
                }else{
                    db('conf')->where(['ename'=>$k])->update(['value'=>$v]);
                }
            }
            return ['msg'=>'配置成功','icon'=>1];
        }
		$siteConfRes=db('conf')->select();
		$this->assign('siteConfRes',$siteConfRes);
		return view();
	}
	
    //文件上传
    //layui图片上传
    public function upload(){
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(CONFIMG);
        if($info){
            $reubfo['code']= 1;
            $reubfo['savename'] = str_replace("\\","/",$info->getSaveName());
        }else{
            // 上传失败获取错误信息
            $reubfo['code']= 0;
            $reubfo['err'] = $file->getError();
        } 
        return json_encode($reubfo);
    }

    //ajax 删除本地图片
    public function cancel(){
        $id=input('id');
        if($id){
           db('conf')->where('id',$id)->setField('value','');
        }
        $imgurl=input('imgurl');
        $res=@unlink(CONFIMG.$imgurl);
        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }
	
	
	
}